home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: pcgpe@ix.netcom.com(Mark Feldman)
- Newsgroups: comp.lang.c++
- Subject: Inheritance problem...and other stuff
- Date: 23 Mar 1996 09:01:07 GMT
- Organization: Netcom
- Message-ID: <4j0ekj$9f6@cloner3.netcom.com>
- NNTP-Posting-Host: chi-il10-16.ix.netcom.com
- X-NETCOM-Date: Sat Mar 23 1:01:07 AM PST 1996
-
-
- Heya,
-
- I remember seeing a C++ FAQ once but I haven't had any luck finding it
- again. If someone could point me to it I'd be grateful.
-
- In the mean time I'll go ahead and ask my questions...at the risk of
- asking something in the FAQ and getting flamed for it :)
-
- Here's a generic form of my problem. (BTW I use BC 3.1 and MSVC 4.0 and
- solutions for either compiler would be fine.) Let's say I have a tree
- of objects like this:
-
- food
- / \
- / \
- fruit vegetable
- / | \ / \
- apple orange banana pea potato
-
-
- Now I maintain an array of pointers to objects, each of which points to
- some type of object in the tree:
-
- foodptr array[ARRAYSIZE];
-
- I've given food two virtual functions, char * name() which is used for
- identification purposes and BOOL isA(name) which I want to use to get
- information about an object. At the moment each object overrides both
- functions.
-
- To print each object's name I simply loop through the array and call
- array[]->name();
-
- What I want to do however is determine info about the object which is
- inherited from it's parents. Thus if my object is of type apple then
- calling apple->isA("apple") should return true. apple->isA("fruit")
- should also return true, but apple->isA("vegetable") should return
- false. If for example I have many different types of apples I want to
- be able to call any object with object->isA("apple") and find out if
- it's at least a type of apple. The overload for apple::isA() would
- therefore look something like this:
-
- BOOL apple::isA(char * type)
- {
- return (!stricmp(name, type)) || fruit::isA(type);
- }
-
- Now the easiest solution would be to simply overload isA for every type
- of class, but my question is if there is any way to avoid this?
- Something like this would make life a bit easier for me:
-
- BOOL fruit::isA(char * type)
- {
- return (!stricmp(name, type)) || Parent::isA(name);
- }
-
- where apple, orange, banana and any of their child classes
- automatically inherit this function and call the appropriate parent. Is
- it possible or even advisable to do something like this?
-
- 2nd Question:
-
- What's the standard way of storing such an array to a file and
- retrieving it? I could always assign an ID number to each class type
- and load them using case statements but this seems messy. I notice that
- MFC and OWL seem to use tables to handle the dispatching of windows
- messages. Would it be feasable to use this for saving the array info to
- a file? Idealy I would like each function to inherit a serialization
- function which they can overload to save and restore their data, but
- how can I make the loading routine know what type of class to create in
- the first place while still keeping the code tight and clean?
-
- Am I going about all this the wrong way? For some time now I've
- considered myself a "good" C++ programmer. When I hit problems like
- this I start to wonder if I'm really nothing more than a C programmer
- with a C++ compiler. Feels like playing Zork on a Pentium.
-
- All help/suggestions/flames etc appreciated.
-
- Cheers,
-
- Mark Feldman
-
-